#include "VSRenderState.h"
#include "VSShader.h"
#include "VSMaterial.h"
#include "VSGraphicInclude.h"
#include "VSResourceManager.h"
#include "VSStream.h"
using namespace VSEngine2;
IMPLEMENT_RTTI(VSRenderState,VSObject)
BEGIN_ADD_PROPERTY(VSRenderState,VSObject)
REGISTER_PROPERTY(m_DepthStencilDesc,DepthStencilDesc,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY(m_RasterizerDesc,RasterizerDesc,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY(m_BlendDesc,BlendDesc,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY(m_RectArray,RectArray,VSProperty::F_SAVE_LOAD_CLONE)
REGISTER_PROPERTY(m_Plane,Plane,VSProperty::F_SAVE_LOAD_CLONE)
END_ADD_PROPERTY
IMPLEMENT_INITIAL_BEGIN(VSRenderState)
IMPLEMENT_INITIAL_END
bool VSRenderState::PostLoad(void * pDate)
{
	m_pDepthStencilState = VSResourceManager::CreateDepthStencilState(m_DepthStencilDesc);
	m_pBlendState = VSResourceManager::CreateBlendState(m_BlendDesc);
	m_pRasterizerState = VSResourceManager::CreateRasterizerState(m_RasterizerDesc);
	return true;
}
void VSRenderState::Inherit(const VSRenderState * pRenderState,unsigned int uiInheritFlag)
{
	if (!uiInheritFlag)
	{
		return ;
	}
	VSMAC_ASSERT(pRenderState);
	bool bReCreateDepthStencil = false;
	bool bReCreateRasterizer = false;
	bool bReCreateBlend = false;
	if (uiInheritFlag & IF_WIRE_ENABLE)
	{
		if (m_pRasterizerState->GetRasterizerDesc().m_bWireEnable != 
			pRenderState->m_pRasterizerState->GetRasterizerDesc().m_bWireEnable)
		{
			bReCreateRasterizer = true;
			m_RasterizerDesc.m_bWireEnable = pRenderState->m_pRasterizerState->GetRasterizerDesc().m_bWireEnable;
		}
	}
	if (bReCreateRasterizer)
	{
		m_pRasterizerState = VSResourceManager::CreateRasterizerState(m_RasterizerDesc);
	}
	if (bReCreateDepthStencil)
	{
		m_pDepthStencilState = VSResourceManager::CreateDepthStencilState(m_DepthStencilDesc);
	}
	if (bReCreateBlend)
	{
		m_pBlendState = VSResourceManager::CreateBlendState(m_BlendDesc);
	}
	return;
}
bool VSRenderState::Save(VSStream & rStream,unsigned int &iSaveUse)const
{

	if (!m_DepthStencilDesc.Save(rStream,iSaveUse))
		return false;

	if(!m_RasterizerDesc.Save(rStream,iSaveUse))
		return false;

	if(!m_BlendDesc.Save(rStream,iSaveUse))
		return false;

	unsigned int  uiPlaneNum = m_Plane.GetNum();

	if (!rStream.Write(&uiPlaneNum,sizeof(unsigned int)))
	{
		return false;
	}
	iSaveUse += sizeof(unsigned int);
	for (unsigned int i = 0 ; i < uiPlaneNum ; i++)
	{
		if (!rStream.Write(&m_Plane[i],sizeof(VSPlane3)))
		{
			return false;
		}
		iSaveUse += sizeof(VSPlane3);
	}

	unsigned int  uiRectNum = m_RectArray.GetNum();
	if (!rStream.Write(&uiRectNum,sizeof(unsigned int)))
	{
		return false;
	}
	iSaveUse += sizeof(unsigned int);
	for (unsigned int i = 0 ; i < uiRectNum ; i++)
	{
		if (!rStream.Write(&m_RectArray[i],sizeof(VSRect2)))
		{
			return false;
		}
		iSaveUse += sizeof(VSRect2);
	}
	return true;
}
bool VSRenderState::Load(VSStream & rStream,unsigned int &iSaveUse)
{

	if (!m_DepthStencilDesc.Load(rStream,iSaveUse))
		return false;

	if(!m_RasterizerDesc.Load(rStream,iSaveUse))
		return false;


	if(!m_BlendDesc.Load(rStream,iSaveUse))
		return false;


	unsigned int  uiPlaneNum = 0;

	if (!rStream.Read(&uiPlaneNum,sizeof(unsigned int)))
	{
		return false;
	}
	iSaveUse += sizeof(unsigned int);
	m_Plane.SetBufferNum(uiPlaneNum);
	for (unsigned int i = 0 ; i < uiPlaneNum ; i++)
	{
		if (!rStream.Read(&m_Plane[i],sizeof(VSPlane3)))
		{
			return false;
		}
		iSaveUse += sizeof(VSPlane3);
	}

	unsigned int  uiRectNum = 0;

	if (!rStream.Read(&uiRectNum,sizeof(unsigned int)))
	{
		return false;
	}
	iSaveUse += sizeof(unsigned int);
	m_RectArray.SetBufferNum(uiRectNum);
	for (unsigned int i = 0 ; i < uiRectNum ; i++)
	{
		if (!rStream.Read(&m_RectArray[i],sizeof(VSRect2)))
		{
			return false;
		}
		iSaveUse += sizeof(VSRect2);
	}
	return true;
}